package com.jneagle.xlstool.bzwgjmx.view.handler;

import com.dwarfeng.dutil.basic.gui.swing.SwingUtil;
import com.dwarfeng.subgrade.stack.handler.Handler;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.util.Locale;

/**
 * @author DwArFeng
 * @since 1.0.0
 */
@Component
public class MessageHandler implements Handler {

    private final I18nHandler i18nHandler;
    private final Locale uiLocale;

    public MessageHandler(
            I18nHandler i18nHandler,
            @Qualifier("uiLocale") Locale uiLocale
    ) {
        this.i18nHandler = i18nHandler;
        this.uiLocale = uiLocale;
    }

    public void warn(java.awt.Component parentComponent, String messageKey) {
        SwingUtil.invokeInEventQueue(() -> {
            JOptionPane.setDefaultLocale(uiLocale);
            JOptionPane.showMessageDialog(
                    parentComponent,
                    i18nHandler.getMessage(messageKey),
                    i18nHandler.getMessage("ui.label.014"),
                    JOptionPane.ERROR_MESSAGE
            );
        });
    }

    public void error(java.awt.Component parentComponent, String messageKey) {
        SwingUtil.invokeInEventQueue(() -> {
            JOptionPane.setDefaultLocale(uiLocale);
            JOptionPane.showMessageDialog(
                    parentComponent,
                    i18nHandler.getMessage(messageKey),
                    i18nHandler.getMessage("ui.label.010"),
                    JOptionPane.WARNING_MESSAGE
            );
        });
    }
}
